package spring.boot.admin.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import spring.boot.admin.mapper.TProvinceMapper;
import spring.boot.admin.model.*;

import java.util.List;

@Slf4j
@Service
@DS("one")
public class TProvinceService extends ServiceImpl<TProvinceMapper, TProvince> {

    @Autowired
    private TCityService tCityService;

    @Autowired
    private TDistrictService tDistrictService;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${amap-url}")
    private String amapUrl;


    /**
     * 初始化省市区数据
     */
    @Transactional
    public String init() {
        remove(null);
        tCityService.remove(null);
        tDistrictService.remove(null);
        JSONObject mapJson = restTemplate.getForObject(amapUrl, JSONObject.class);
        JSONArray countryArray = mapJson.getJSONArray("districts");
        for (int i = 0; i < countryArray.size(); i++) {
            JSONObject country = JSON.parseObject(JSON.toJSONString(countryArray.get(i)));
            JSONArray provinceArray = country.getJSONArray("districts");
            for (int j = 0; j < provinceArray.size(); j++) {
                JSONObject provinceJson = JSON.parseObject(JSON.toJSONString(provinceArray.get(j)));
                TProvince province = new TProvince();
                province.setCode(provinceJson.getString("adcode"));
                province.setName(provinceJson.getString("name"));
                save(province);
                JSONArray cityArray = provinceJson.getJSONArray("districts");
                for (int k = 0; k < cityArray.size(); k++) {
                    JSONObject cityJson = JSON.parseObject(JSON.toJSONString(cityArray.get(k)));
                    TCity city = new TCity();
                    city.setPid(province.getId());
                    city.setCode(cityJson.getString("adcode"));
                    city.setPcode(provinceJson.getString("adcode"));
                    city.setName(cityJson.getString("name"));
                    tCityService.save(city);
                    JSONArray districtArray = cityJson.getJSONArray("districts");
                    for (int m = 0; m < districtArray.size(); m++) {
                        JSONObject districtJson = JSON.parseObject(JSON.toJSONString(districtArray.get(m)));
                        TDistrict district = new TDistrict();
                        district.setPid(city.getId());
                        district.setCode(districtJson.getString("adcode"));
                        district.setPcode(cityJson.getString("adcode"));
                        district.setName(districtJson.getString("name"));
                        tDistrictService.save(district);
                    }
                }
            }
        }
        return "1";
    }

    public IPage pageList(Integer page, Integer rows) {
        //为适应 easyui的分页插件,稍微修改原参数名称，自定义Pages
        Pages<TProvince> tProvincePages = new Pages<>(page, rows);
        QueryWrapper<TProvince> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("code");
        return page(tProvincePages, wrapper);
    }

    public List<TProvince> selectList() {
        QueryWrapper<TProvince> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("code");
        return list(wrapper);
    }
}
